//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_org_member_api_serv

import (
	"context"

	"atomgit.com/openlinksaas/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas/api-server/config"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/org_dao"
	"atomgit.com/openlinksaas/api-server/dao/user_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/org_member_api"
)

type OrgMemberAdminApiImpl struct {
	org_member_api.UnimplementedOrgMemberAdminApiServer
	adminCfg *config.AdminCfg
}

func NewOrgMemberAdminApiImpl(adminCfg *config.AdminCfg) *OrgMemberAdminApiImpl {
	return &OrgMemberAdminApiImpl{adminCfg: adminCfg}
}

func (apiImpl *OrgMemberAdminApiImpl) List(ctx context.Context, req *org_member_api.AdminListRequest) (*org_member_api.AdminListResponse, error) {
	emptyList := []*org_member_api.MemberInfo{}

	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &org_member_api.AdminListResponse{
			Code:           org_member_api.AdminListResponse_CODE_WRONG_SESSION,
			ErrMsg:         "会话不存在",
			MemberInfoList: emptyList,
		}, nil
	}
	if !sess.AuthOk {
		return &org_member_api.AdminListResponse{
			Code:           org_member_api.AdminListResponse_CODE_NOT_AUTH,
			ErrMsg:         "未授权会话",
			MemberInfoList: emptyList,
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.OrgMemberPerm.Read {
		return &org_member_api.AdminListResponse{
			Code:           org_member_api.AdminListResponse_CODE_NO_PERMISSION,
			ErrMsg:         "没有权限",
			MemberInfoList: emptyList,
		}, nil
	}

	exist, err := org_dao.OrgInfoDao.Exist(ctx, req.OrgId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &org_member_api.AdminListResponse{
			Code:           org_member_api.AdminListResponse_CODE_NO_ORG,
			ErrMsg:         "组织不存在",
			MemberInfoList: emptyList,
		}, nil
	}

	memberItemList, err := org_dao.MemberDao.ListByOrg(ctx, req.OrgId)
	if err != nil {
		return nil, err
	}
	//补全用户信息
	userIdList := []string{}
	for _, memberItem := range memberItemList {
		userIdList = append(userIdList, memberItem.MemberUserId)
	}
	userItemList, err := user_dao.UserInfoDao.ListById(ctx, userIdList)
	if err != nil {
		return nil, err
	}
	userItemMap := map[string]*user_dao.UserInfoDbItem{}
	for _, userItem := range userItemList {
		userItemMap[userItem.UserId] = userItem
	}
	//生成结果
	memberList := []*org_member_api.MemberInfo{}
	for _, memberItem := range memberItemList {
		member := memberItem.ToMemberInfo()
		userItem, ok := userItemMap[memberItem.MemberUserId]
		if ok {
			member.DisplayName = userItem.BasicInfo.DisplayName
			member.LogoUri = userItem.BasicInfo.LogoUri
		}
		member.DepartMentIdPath = []string{} //不计算部门目录
		memberList = append(memberList, member)
	}

	return &org_member_api.AdminListResponse{
		Code:           org_member_api.AdminListResponse_CODE_OK,
		MemberInfoList: memberList,
	}, nil
}
